using System;
using System.Text;
using HipChat;
using Wolfpack.Core;
using Wolfpack.Core.Interfaces.Entities;

namespace Wolfpack.Contrib.Publishers.HipChat.Formatters
{
    public class AgentStartFormatter : MessageFormatterBase
    {
        public AgentStartFormatter(HipChatPublisherConfig config) : base(config)
        {
        }

        public override bool AppliesToNotification(NotificationEvent notification)
        {
            return notification.EventType.Equals(NotificationEventAgentStart.EventTypeName,
                StringComparison.OrdinalIgnoreCase);
        }

        public override HipChatMessage Format(HipChatMessage message, NotificationEvent notification)
        {
            var startNotification = Serialiser.FromJson<NotificationEventAgentStart>(notification.Data);

            SetMessageText(message, startNotification);
            SetMessageFrom(message, startNotification);
            SetMessageBackground(message, startNotification);
            return message;
        }

        protected virtual void SetMessageText(HipChatMessage message, NotificationEventAgentStart notification)
        {
            var textBuilder = new StringBuilder(string.Format("Agent {0} on {1} started{2}",
                notification.AgentId,
                notification.SiteId, 
                CRLF));

            if ((notification.Activities != null) && (notification.Activities.Count > 0))
            {
                textBuilder.AppendFormat("{0} Activities loaded...{1}", notification.Activities.Count, CRLF);
                notification.Activities.ForEach(c => textBuilder.AppendFormat("{0}{1}", c.Name, CRLF));
            }
            if ((notification.UnhealthyActivities != null) && (notification.UnhealthyActivities.Count > 0))
            {
                textBuilder.AppendFormat("<b>*** ATTENTION ***</b>{0}", CRLF);
                textBuilder.AppendFormat("{0} Unheathly Activities...{1}", notification.UnhealthyActivities.Count, CRLF);
                notification.UnhealthyActivities.ForEach(c => textBuilder.AppendFormat("{0}{1}", c.Name, CRLF));
            }
            if ((notification.Checks != null) && (notification.Checks.Count > 0))
            {
                textBuilder.AppendFormat("{0} Checks loaded...{1}", notification.Checks.Count, CRLF);
                notification.Checks.ForEach(c => textBuilder.AppendFormat("{0}{1}", c.Name, CRLF));
            }
            if ((notification.UnhealthyChecks != null) && (notification.UnhealthyChecks.Count > 0))
            {
                textBuilder.AppendFormat("<b>*** ATTENTION ***</b>{0}", CRLF);
                textBuilder.AppendFormat("{0} Unheathly Checks...{1}", notification.UnhealthyChecks.Count, CRLF);
                notification.UnhealthyChecks.ForEach(c => textBuilder.AppendFormat("{0}{1}", c.Name, CRLF));
            }

            message.Text = textBuilder.ToString();
        }

        private void SetMessageFrom(HipChatMessage message, NotificationEventAgentStart notification)
        {
            message.From = GetFrom(notification.AgentId, notification.SiteId);
        }

        protected virtual void SetMessageBackground(HipChatMessage message, NotificationEventAgentStart notification)
        {
            if ((notification.UnhealthyActivities != null) && (notification.UnhealthyActivities.Count > 0))
            {
                message.BackgroundColor = HipChatClient.BackgroundColor.red;
                return;
            }
            if ((notification.UnhealthyChecks != null) && (notification.UnhealthyChecks.Count > 0))
            {
                message.BackgroundColor = HipChatClient.BackgroundColor.red;
                return;
            }

            message.BackgroundColor = HipChatClient.BackgroundColor.green;
        }
    }
}